<template>
  <div class="list-table">
    <div class="nospecialModel ">
      <div class="area-header">
        <span class="area-header-title">{{ $t('项目团队成员') }}</span>
      </div>
      <el-form v-if="this.$route.query.id" ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item :label="$t('用户名称')" prop="user.userId">
          <UserSelect
            v-model="queryParams.user.userId"
            :placeholder="$t('用户名称')"
          />
        </el-form-item>
        <el-form-item :label="$t('参与角色')" prop="role">
          <el-select v-model="queryParams.role" :placeholder="$t('参与角色')" clearable size="small">
            <el-option
              v-for="dict in roleOptions"
              :key="dict.dictValue"
              :label="$t(dict.dictLabel)"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('参与时间')" prop="joinTime">
          <DatePicker v-model="joinTimes" type="daterange" />
        </el-form-item>
        <el-form-item :label="$t('是否离开')" prop="leave">
          <el-select v-model="queryParams.leave" :placeholder="$t('是否离开')" clearable size="small">
            <el-option
              v-for="dict in leaveOptions"
              :key="dict.dictValue"
              :label="$t(dict.dictLabel)"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['deal:team:edit']"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="addHandle"
          >{{ $t("新增") }}</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['deal:team:export']"
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="exportHandle"
          >{{ $t("导出") }}</el-button>
        </el-col>
      </el-row>
    </div>

    <div class="specialModel">
      <!-- 不可删除 -->
      <div class="area-header left">
        <span class="area-header-title">{{ $t('项目团队成员') }}</span>
      </div>
      <div class="right">
        <el-button
          v-hasPermi="['deal:team:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </div>
    </div>

    <el-table
      ref="table"
      v-loading="loading"
      tooltip-effect="darkTable"
      :data="dealTeamList"
      border
      @cell-mouse-enter.once="rowDrop"
    >
      <el-table-column label="" align="center" width="50">
        <div class="handle">
          <i class="el-icon-rank link-type" />
        </div>
      </el-table-column>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('用户名称')" align="center" prop="user.nickName">
        <template slot-scope="scope">
          <el-link class="link_a" type="primary" @click="updateHandle(scope.row)">{{ scope.row.user ? scope.row.user.nickName : '' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('参与角色')" align="center" prop="role" :formatter="roleFormat" />
      <el-table-column :label="$t('参与时间')" align="center" prop="joinTime" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.joinTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('是否离开')" align="center" prop="leave" :formatter="leaveFormat" width="100" />
      <el-table-column :label="$t('离开时间')" align="center" prop="leaveTime" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaveTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('备注')" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column :label="$t('操作')" fixed="right" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:team:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目团队成员对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealTeamForm ref="dealTeamForm" :deal-id="dealId" />
    </el-dialog>
  </div>
</template>

<script>
import { listDealTeam, delDealTeam, saveTeamList } from '@/api/deal/team/dealTeam'
import dealTeamForm from './dealTeamForm.vue'
import Sortable from 'sortablejs'
export default {
    components: {
        dealTeamForm
    },
  props: {
      dealId: {
        type: String,
        default: undefined
      }
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 项目团队成员表格数据
          dealTeamList: [],
          // 弹出层标题
          title: '项目团队成员',
          // 是否显示弹出层
          formOpen: false,
          joinTimes: [],
          // 参与角色 字典deal_team_role字典
          roleOptions: [],
          // 是否离开 字典 sys_yes_no字典
          leaveOptions: [],
          // 查询参数
          queryParams: {
              user: {
                userId: ''
              },
              dealId: this.$route.query.id ? this.$route.query.id : this.dealId,
              role: undefined,
              joinTime: undefined,
              leave: undefined,
              leaveTime: undefined,
              startTime: undefined,
              endTime: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 保存列表参数
          saveTeamListParam: {
            pageNum: undefined,
            pageSize: undefined,
            list: []
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
          }
      }
  },
  watch: {
    dealId: {
      handler() {
        this.queryParams.dealId = this.$route.query.id ? this.$route.query.id : this.dealId
        this.getList()
      }
    }
  },
  created() {
      this.getList()
      this.getDicts('deal_team_role').then(response => {
          this.roleOptions = response.data
      })
      this.getDicts('sys_yes_no').then(response => {
          this.leaveOptions = response.data
      })
  },
  methods: {
    /* init(dealId) {
    }, */
    /** 查询项目团队成员列表 */
    getList() {
        this.loading = true
        if (this.joinTimes != null && this.joinTimes.length > 1) {
          this.queryParams.startDate = this.joinTimes[0]
          this.queryParams.endDate = this.joinTimes[1]
        } else {
          this.queryParams.startDate = undefined
          this.queryParams.endDate = undefined
        }
        listDealTeam(this.queryParams).then(response => {
            this.dealTeamList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 参与角色 字典deal_team_role字典翻译
    roleFormat(row) {
      return this.selectDictLabel(this.roleOptions, row.role)
    },
    // 是否离开 字典 sys_yes_no字典翻译
    leaveFormat(row) {
      return this.selectDictLabel(this.leaveOptions, row.leave)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            user: {
              userId: undefined,
              userName: undefined,
              nickName: undefined
            },
            dealId: undefined,
            role: undefined,
            joinTime: undefined,
            leave: '0',
            leaveTime: undefined,
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.joinTimes = []
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealTeamForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealTeamForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delDealTeam(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcelPost('/team/dealTeam/export', this.queryParams)
    },
    rowDrop() {
      // 行拖拽
      const _this = this
      const tbody2 = this.$refs.table.$el.querySelector('.el-table__body-wrapper tbody')
      Sortable.create(tbody2, {
        handle: '.handle',
        animation: 500,
        fallbackOnBody: true,
        onChoose() {
          // 选择元素
          _this.dealTeamList[_this.dealTeamList.length - 1].fixed = false
        },
        onUnchoose: function(evt) {
          // 取消选择元素
          if (evt) {
            _this.dealTeamList[_this.dealTeamList.length - 1].fixed = 'right'
          }
        },
        onEnd(evt) {
          _this.dealTeamList.splice(evt.newIndex, 0, _this.dealTeamList.splice(evt.oldIndex, 1)[0])
          var newArray = _this.dealTeamList.slice(0)
          _this.dealTeamList = []
          _this.$nextTick(() => {
            _this.dealTeamList = newArray
            _this.dealTeamList.forEach((item, index) => {
              item.rowSort = index + 1
            })
            _this.saveTeamList(_this.dealTeamList)
          })
        }
      })
      this.$forceUpdate()
    },
    saveTeamList(dealTeamList) {
      this.saveTeamListParam.list = dealTeamList
      this.saveTeamListParam.pageNum = this.queryParams.pageNum
      this.saveTeamListParam.pageSize = this.queryParams.pageSize
      saveTeamList(this.saveTeamListParam).then(response => {
        if (response.code === 200) {
          this.msgSuccess(this.$t('保存成功'))
          // this.getList()
        }
      })
    }

  }
}
</script>
